package box.alloc;

import db.*;

import org.osoa.sca.annotations.Service;
import org.osoa.sca.annotations.Reference;

import java.sql.*;
import java.util.ArrayList;

@Service(OrderPlanImportService.class)
public class OrderPlanImportServiceImpl implements OrderPlanImportService {
	private DataAccessService dataAccess;
	
	@Reference
	public void setDataAccessService(DataAccessService dataAccess) {
		this.dataAccess = dataAccess;
	}
	
	public boolean post(OrderPlan orderPlan) {
		boolean res = true;
		
		//若该记录已经存在，返回false表示添加记录不成功
		if (exist(orderPlan)) {
			System.out.println("Data already exists!");
			res = false;
		} else {
			String sql = "insert into OrderPlan values ('', '" +
				orderPlan.getID() + "', '" +
				orderPlan.getScdID() + "', '" +
				orderPlan.getStartTime() + "', '" +
				orderPlan.getEndTime() + "', '" +
				orderPlan.getCancelTime() + "', '" +
				orderPlan.getTotalBox() + "', '" +
				orderPlan.getBigBox() + "')";
			dataAccess.update(sql);
		}
		return res;
	}
	/**
	 * 确保各数据项都有意义
	 * @param orderPlan
	 * @return
	 */
	public boolean exist(OrderPlan orderPlan) {
		//确保传进来的参数合法
		//assert orderPlan.valid() == true;
		//此处先不进行参数合法性检查，待以后添加
		
		boolean res = false;
		
		String sql = "select count(*) from OrderPlan " +
				"where OrderPlan_ID = '" + orderPlan.getID() + "'" +
				" and Scd_ID = '" + orderPlan.getScdID() + "'" +
				" and OrderPlan_StartTime = '" + orderPlan.getStartTime() + "'" +
				" and OrderPlan_EndTime = '" + orderPlan.getEndTime() + "'" +
				" and OrderPlan_CancelTime = '" + orderPlan.getCancelTime() + "'" +
				" and OrderPlan_TotalBox = '" + orderPlan.getTotalBox() + "'" +
				" and OrderPlan_BigBox = '" + orderPlan.getBigBox() + "'";
		
		try {
			ResultSet rs = dataAccess.query(sql);
			rs.next();
			if (rs.getInt(1) > 0) {
				res = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}
	public OrderPlan[] query(OrderPlan orderPlan) {
		String sql = "select * from OrderPlan";
		boolean where = false;
		
		String cond = "";
		if (orderPlan.getID() != null) {
			where = true;
			cond += "OrderPlan_ID = '" + orderPlan.getID() + "' and ";
		}
		if (orderPlan.getScdID() != null) {
			where = true;
			cond += "Scd_ID = '" + orderPlan.getScdID() + "' and ";
		}
		if (orderPlan.getStartTime() != null) {
			where = true;
			cond += "OrderPlan_StartTime = '" + orderPlan.getStartTime() + "' and ";
		}
		if (orderPlan.getEndTime() != null) {
			where = true;
			cond += "OrderPlan_EndTime = '" + orderPlan.getEndTime() + "' and ";
		}
		if (orderPlan.getCancelTime() != null) {
			where = true;
			cond += "OrderPlan_CancelTime = '" + orderPlan.getCancelTime() + "' and ";
		}
		if (orderPlan.getTotalBox() > 0) {
			where = true;
			cond += "OrderPlan_TotalBox = '" + orderPlan.getTotalBox() + "' and ";
		}
		if (orderPlan.getBigBox() > 0) {
			where = true;
			cond += "OrderPlan_BigBox = '" + orderPlan.getBigBox() + "' and ";
		}
		
		if (where == true) {
			sql += " where " + cond.substring(0, cond.lastIndexOf("and") - 1);
		}
		
		ResultSet rs = dataAccess.query(sql);
		
		ArrayList<OrderPlan> resArrayList = new ArrayList<OrderPlan>();
		try {
			while (rs.next()) {
				OrderPlan item = new OrderPlan(rs.getString("OrderPlan_ID"), 
						rs.getString("Scd_ID"),
						rs.getString("OrderPlan_StartTime"),
						rs.getString("OrderPlan_EndTime"),
						rs.getString("OrderPlan_CancelTime"),
						rs.getInt("OrderPlan_TotalBox"),
						rs.getInt("OrderPlan_BigBox"));
				resArrayList.add(item);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return resArrayList.toArray(new OrderPlan[resArrayList.size()]);
	}
}
